/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import type { App } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHashHistory, createWebHistory, RouteRecordName } from 'vue-router'
import type { VabRouteRecordRaw } from './types'
import { authentication, base, isHashRouterMode } from '/@/config'
import { setupPermissions } from '/@/router/permissions'
import Layout from '/@vab/layouts/index.vue'

export const constantRoutes : VabRouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('/@/views/login/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('/@/views/register/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/redirect',
    name: 'Redirect',
    component: () => import('/@/views/redirect/Redirect.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/403',
    name: '403',
    component: () => import('/@/views/error/403.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('/@/views/error/404.vue'),
    meta: {
      hidden: true,
    },
  },
]

export const asyncRoutes : VabRouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    // redirect: 'index',
    component: Layout,
    meta: {
      title: '首页',
      icon: 'home-2-line',
    },
    children: [{
      path: 'statistics',
      name: 'statistics',
      component: () => import('/@/views/NewPage/Home/statistics/index.vue'),
      meta: {
        title: '数据统计',
        icon: 'database-2-line',
      },
    }]
  }, {
    path: '/user',
    name: 'User',
    component: Layout,
    meta: {
      title: '用户管理',
      icon: 'code-box-line',
    },
    children: [{
      path: 'users',
      name: 'users',
      component: () => import('/@/views/NewPage/user/users/index.vue'),
      meta: {
        title: '用户列表',
        icon: 'database-2-line',
      }
    },{
      path: 'orders',
      name: 'orders',
      component: () => import('/@/views/NewPage/user/orders/index.vue'),
      meta: {
        title: '订单列表',
        icon: 'database-2-line',
      }
    }]
  }, {
    path: '/lug',
    name: 'Lug',
    component: Layout,
    meta: {
      title: '搭子管理',
      icon: 'code-box-line',
    },
    children: [{
      path: 'AuditList',
      name: 'AuditList',
      component: () => import('/@/views/NewPage/lug/AuditList/index.vue'),
      meta: {
        title: '搭子列表',
        icon: 'database-2-line',
      }
    }]
  }, {
    path: '/dynamics',
    name: 'Dynamics',
    component: Layout,
    meta: {
      title: '动态管理',
      icon: 'code-box-line',
    },
    children: [{
      path: 'dynamicsAuditList',
      name: 'dynamicsAuditList',
      component: () => import('/@/views/NewPage/dynamics/AuditList/index.vue'),
      meta: {
        title: '动态列表',
        icon: 'database-2-line',
      }
    }]
  }, {
    path: '/system',
    name: 'System',
    component: Layout,
    meta: {
      title: '系统配置',
      icon: 'code-box-line',
    },
    children: [{
      path: 'swipers',
      name: 'swipers',
      component: () => import('/@/views/NewPage/system/swipers/index.vue'),
      meta: {
        title: '轮播图管理',
        icon: 'database-2-line',
      },
    }, {
      path: 'server',
      name: 'server',
      component: () => import('/@/views/NewPage/system/server/index.vue'),
      meta: {
        title: '客服管理',
        icon: 'database-2-line',
      },
    },{
      path: 'tag',
      name: 'tag',
      component: () => import('/@/views/NewPage/system/tag/index.vue'),
      meta: {
        title: '兴趣标签管理',
        icon: 'database-2-line',
      },
    },{
      path: 'topicTag',
      name: 'topicTag',
      component: () => import('/@/views/NewPage/system/topicTag/index.vue'),
      meta: {
        title: '话题标签管理',
        icon: 'database-2-line',
      },
    }]
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    name: 'NotFound',
    meta: {
      title: '404',
      hidden: true,
    },
  },
]

const router = createRouter({
  history: isHashRouterMode ? createWebHashHistory(base) : createWebHistory(base),
  routes: constantRoutes as RouteRecordRaw[],
})

const fatteningRoutes = (routes : VabRouteRecordRaw[]) : VabRouteRecordRaw[] => {
  return routes.flatMap((route) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

const addRouter = (routes : VabRouteRecordRaw[]) => {
  routes.forEach((route : VabRouteRecordRaw) => {
    if (!router.hasRoute(route.name)) router.addRoute(route as RouteRecordRaw)
    if (route.children) addRouter(route.children)
  })
}

export const resetRouter = (routes : VabRouteRecordRaw[] = constantRoutes) => {
  routes.map((route : VabRouteRecordRaw) => {
    if (route.children) route.children = fatteningRoutes(route.children)
  })
  router.getRoutes().forEach((route) => {
    if (route.name) {
      const routeName : RouteRecordName = route.name
      router.hasRoute(routeName) && router.removeRoute(routeName)
    }
  })
  addRouter(routes)
}

export const setupRouter = (app : App<Element>) => {
  if (authentication === 'intelligence') addRouter(asyncRoutes)
  setupPermissions(router)
  app.use(router)
  return router
}

export default router
